#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main()
{
	int g_val = 100; // 全局变量
    printf("Father process is running, pid: %d, ppid: %d\n", getpid(), getppid());
	sleep(5);
    
	// 创建进程
	pid_t id = fork();
	if (id == 0)
	{
		// child
		int cnt = 0;
		while(1)
		{
			if (cnt == 5)
             {
              	g_val = 300; // 运行 5 秒，子进程修改全局变量
                printf("I am child process, g_val: 100->300\n");
             }
			printf("I am child process, pid: %d, ppid: %d, g_val: %d, &g_val: %p\n", getpid(), getppid(), g_val, &g_val);
			cnt++;
			sleep(1);
		}
	}
	else
	{
		// father
		while(1)
		{
			printf("I am father process, pid: %d, ppid: %d, g_val: %d, &g_val: %p\n", getpid(), getppid(), g_val, &g_val);
			sleep(1);
		}
	}
	return 0;
}
